package com.tlibrary.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.tlibrary.model.User;
import com.tlibrary.service.AdminService;
import com.tlibrary.util.ExcelUtil;
import com.tlibrary.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.*;

/**
 * @Author：wcc
 * @Description：
 * @Date：Create in: 2019/1/9--17:18
 */
@Controller
@RequestMapping("/admin")
public class adminController {

    @Autowired
    private ExcelUtil excelUtil;
    @Autowired
    private AdminService as;
    @RequestMapping("/find")
    public void finduser(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        HashMap map=new HashMap();
        map.put("type",request.getParameter("zhlx"));
        map.put("accountid",request.getParameter("dlzh"));
        map.put("username",request.getParameter("userName"));
        HashMap returnMap =as.finduser(pageNum, pageSize,map);
        Gson gson = new Gson();
        String json = gson.toJson(returnMap);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/saveAdduser")
    public @ResponseBody Object saveAdduser(HttpServletRequest request,HttpServletResponse response){
        String dlzh = request.getParameter("dlzh");
        String userName = request.getParameter("userName");
        String dept = request.getParameter("dept");
        String major = request.getParameter("major");
        String type = request.getParameter("type");
        User user=new User();
        user.setAccountid(dlzh);
        user.setUsername(userName);

        user.setType(type);
        String msg=as.saveAdduser(user);

        return  msg;
    }

    @RequestMapping("/recoveryuserPWD")
    public void  recoveryuserPWD(HttpServletRequest request,HttpServletResponse response){
        String id=request.getParameter("id");
        String msg="复原密码成功";
        try {
            as.recoveryuserPWD(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
    @RequestMapping("/delUser")
    public @ResponseBody String delUser(HttpServletRequest request){
        String datas = request.getParameter("datas");
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        JSONArray array = JSONObject.parseArray(datas);
        int i = as.delUser(array);
        if (i==0){
            return "删除失败";
        }else {
            return "删除成功";
        }

    }
    @RequestMapping("/downloadTemplateExcel")
    public void downloadTemplateExcel(HttpServletRequest request, HttpServletResponse response){

        List list=null;
        String[] tableTitle = {"用户账号","姓名","密码"};
        String[] fields={ "accountid","username","password"};
        String tableName = "用户导入模板.xls";
        try {
            excelUtil.exportExcelCommon(request, response, list, tableTitle, fields, tableName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 批量新增用户
     * @param file
     * @param response
     */
    @RequestMapping("/addeUsers")
    public @ResponseBody
    void addeUsers(@RequestParam(value="excel") MultipartFile file, HttpServletResponse response,HttpServletRequest request){

        Map map=new HashMap<>();
        String[] parameter = {"accountid","username","password"};
        List list =null;
        String msg="上传成功";
        try {
             list = excelUtil.readXls(file, parameter);
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
            msg="上传失败";
        }
            int i =as.addeUsers(list);
        if (i!=0){
            Gson gson = new Gson();
            String json = gson.toJson(msg);
            //将数据发送到前端
            System.out.println(json);
            ResponseUtil.toHtml(response, json);
        }
        System.out.println(i);


    }
    @RequestMapping("/findDept")
    public void findDept(HttpServletRequest request, HttpServletResponse response){

        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        HashMap hashMap=new HashMap();
        HashMap returnMap =as.findDept(pageNum, pageSize,hashMap);
        Gson gson = new Gson();
        String json = gson.toJson(returnMap);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/findCol")
    public void findCol(HttpServletRequest request, HttpServletResponse response){
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        HashMap hashMap=new HashMap();
        HashMap returnMap =as.findCol(pageNum, pageSize,hashMap);
        Gson gson = new Gson();
        String json = gson.toJson(returnMap);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
    @RequestMapping("/addCol")
    public @ResponseBody Object addCol(HttpServletRequest request, HttpServletResponse response){
        String colname = request.getParameter("colname");
      //  String userid=request.getSession().getAttribute("id").toString();
        String userid="fd3affc2-e956-4954-9e66-44e0baba01d3";
        try {
            as.addCol(colname,userid);
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败";

        }
        return "添加成功";
    }
    @RequestMapping("/editCol")
    public @ResponseBody Object editCol(HttpServletRequest request){
        int i=0;
        String colname = request.getParameter("colname");
        String colnameid = request.getParameter("colnameid");
        //  String userid=request.getSession().getAttribute("id").toString();
        String userid="fd3affc2-e956-4954-9e66-44e0baba01d3";
        try {
             i=as.editcol(colname,colnameid,userid);
        } catch (Exception e) {
            e.printStackTrace();
            return "修改失败";
        }
        if (i==1){
            return "修改成功";
        }else {
            return "修改失败";
        }


    }
    @RequestMapping("/delDept")
    public @ResponseBody Object delDept(HttpServletRequest request){

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        String datas = request.getParameter("datas");
        JSONArray array = JSONObject.parseArray(datas);
        try {
            as.deldept(array);
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
        return "删除成功";
    }
    @RequestMapping("/delCol")
    public @ResponseBody Object delCol(HttpServletRequest request){

        String datas = request.getParameter("datas");
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        JSONArray array = JSONObject.parseArray(datas);
        try {
            as.delCol(array);
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
        return "删除成功";
    }

    @RequestMapping("/adddept")
    public @ResponseBody Object adddept(HttpServletRequest request){
        //  String userid=request.getSession().getAttribute("id").toString();
        String userid="fd3affc2-e956-4954-9e66-44e0baba01d3";
        String colnameid = request.getParameter("colnameid");
        String dept = request.getParameter("dept");
        HashMap hashMap=new HashMap();
        hashMap.put("userid",userid);
        hashMap.put("colnameid",colnameid);
        hashMap.put("dept",dept);
        try {
            as.addDept(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败";
        }
        return "添加成功";
    }
    @RequestMapping("/editDept")
    public @ResponseBody Object editDept (HttpServletRequest request){
        //  String userid=request.getSession().getAttribute("id").toString();
        String userid="fd3affc2-e956-4954-9e66-44e0baba01d3";
        String editdeptname = request.getParameter("editdeptname");
        String editdepid = request.getParameter("editdepid");
        String Secteurid = request.getParameter("Secteurid");
        HashMap hashMap=new HashMap();
        hashMap.put("userid",userid);
        hashMap.put("editdeptname",editdeptname);
        hashMap.put("editdepid",editdepid);
        hashMap.put("Secteurid",Secteurid);
        hashMap.put("time",new Date());
        try {
            as.editDept(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "修改成功";
    }

}
